<?php

namespace app\admin\controller;

use think\facade\Cache;
use think\facade\View;

class Test extends Base
{
    public function index()
    {
        $task = [
            [
                'title'  => '静态释放',
                'type'   => 'release',
                'record' => $this->getRecord('release')
            ],
            [
                'title'  => '联创分红',
                'type'   => 'shopBonus',
                'record' => $this->getRecord('founderBonus')
            ],
            [
                'title'  => '全网总数累加',
                'type'   => 'incFoodNum',
                'record' => $this->getRecord('incFoodNum')
            ]
        ];

        View::assign([
            'task' => $task
        ]);
        return View::fetch();
    }

    public function getRecord($type)
    {
        $list = Cache::get('task_' . $type);
        if (empty($list)) {
            return [];
        }
        $sort = [];
        foreach ($list as &$v) {
            $sort[] = $v['times'];
        }
        array_multisort($sort, SORT_DESC, $list);
        return $list;
    }

    public function execute()
    {
        try {

            $param = $this->request->post();

            $t1 = microtime(true);
            switch ($param['type']) {
                case "release":
                    $result = \app\common\model\TradeIssue::release();
                    break;
                case "founderBonus":
                    $result = \app\common\model\RaiseRecord::founderBonus(true);
                    break;
                case "incFoodNum":
                    $result = \app\common\model\TradeIssue::incFoodNum();
                    break;
            }
            if ($result['code'] == 1) {
                $msg = 'success';
            } else {
                $msg = 'error：' . $result['msg'];
            }
            $t2 = microtime(true);
            $data['admin'] = $this->admin['admin_name'];
            $data['times'] = time();
            $data['method'] = $this->request->action();
            $data['ip'] = $this->request->ip();
            $data['required'] = round($t2 - $t1, 6);
            $data['status'] = $msg;
            $list = Cache::get('task_' . $param['type']);
            $list[] = $data;
            Cache::set('task_' . $param['type'], $list);
            return json($result);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }

}

